const Router = require('koa-router');  
const router = new Router();  
const koaBody = require('koa-bodyparser');  
const bcrypt = require('bcrypt'); // 引入bcrypt库  
const saltRounds = 10; // 设置bcrypt的salt轮数，即加密复杂度  
  
const lrx = require('../model/LRX');  
  
router.prefix('/own');

router.post('/register', koaBody(), async ctx => {  
    const user_name = ctx.request.body.user_name;  
    const password = ctx.request.body.password;  
    const e_mail = ctx.request.body.e_mail;  
  
    // 使用bcrypt对密码进行哈希处理  
    const hashedPassword = await bcrypt.hash(password, saltRounds);  
  
    // 将加密后的密码存储在数据库中  
    const rows = await lrx.REGISTER(e_mail, user_name, hashedPassword);  
    ctx.body = rows;  
});  

router.post('/debark', koaBody(), async ctx => {    
    const identifier = ctx.request.body.user_name; // 可以是用户名或邮箱  
    const password = ctx.request.body.password;    
  
    // 假设 getUserByUsernameOrEmail 是一个根据用户名或邮箱检索用户的方法  
    const user = await lrx.GETUSERBYUSERNAMEOREMAIL(identifier);    
  
    if (user) {    
        // 使用bcrypt比较用户输入的密码和数据库中存储的哈希密码是否匹配    
        const match = await bcrypt.compare(password, user.password); // 假设user对象中包含哈希后的密码    
  
        if (match) {    
            ctx.body = { success: true, msg: '登录成功', user: user }; // 添加 msg 属性
            // console.log(password, user.password)    
        } else {    
            // console.log(password, user.password) 
            ctx.body = { success: false, msg: '密码错误' }; // 修改 message 为 msg    
        }    
    } else {    
        // 用户不存在    
        ctx.body = { success: false, msg: '用户不存在！' };    
    }    
});  


router.post('/searchpwd', koaBody(), async ctx => {      
    const userId = ctx.request.body.userId;  
    const password = ctx.request.body.password;  
  
    const user = await lrx.GETUSERBYID(userId);  
    if (user) {      
        const match = await bcrypt.compare(password, user.password);  
  
        if (match) {      
            ctx.body = { success: true, msg: '登录成功', user: user };  
        } else {      
            ctx.body = { success: false, msg: '密码错误' };  
        }      
    } else {      
        // console.log(user)
        ctx.body = { success: false, msg: '用户不存在！' };      
    }      
});


router.post('/updatepwd', koaBody(), async ctx => {  
    const userId = ctx.request.body.userId; // 接收用户ID  
    const newPassword = ctx.request.body.newPassword; // 接收新密码  
  
  
    // 使用bcrypt对新密码进行哈希处理  
    const hashedNewPassword = await bcrypt.hash(newPassword, saltRounds); 

  
    // 更新数据库中的密码  
    const updated = await lrx.UPDATEPASSWORD(userId, hashedNewPassword); // 假设此方法存在并正确实现了密码更新  
    // console.log(updated.success);
    if (updated.success) {  
        ctx.body = { success: true, msg: '密码更新成功！' };  
    } else {  
        // console.log(hashedNewPassword)
        ctx.body = { success: false, msg: '密码更新失败！' };  
    }  
});  

router.post('/updateusername', koaBody(), async ctx => {  
    const userId = ctx.request.body.userId; // 接收用户ID  
    const newUsername = ctx.request.body.newUsername; 
  
    // 更新数据库中的用户名
    const updated = await lrx.UPDATEUSERNAME(userId, newUsername); // 假设此方法存在并正确实现了密码更新  
    // console.log(updated.success);
    if (updated.success) {  
        ctx.body = { success: true, msg: '用户名更新成功！' };  
    } else {  
        // console.log(newUsername)
        ctx.body = { success: false, msg: '用户名更新失败！' };  
    }  
});  

router.post('/updateemail', koaBody(), async ctx => {  
    const userId = ctx.request.body.userId; // 接收用户ID  
    const newEmail = ctx.request.body.newEmail; 
  
    // 更新数据库中的用户名
    const updated = await lrx.UPDATEEMAIL(userId, newEmail);  
    // console.log(updated.success);
    console.log(newEmail)
    if (updated.success) {  
        ctx.body = { success: true, msg: '邮箱更新成功！' };  
    } else {  
        // console.log(newEmail)
        ctx.body = { success: false, msg: '邮箱更新失败！' };  
    }  
}); 

router.post('/updatesex', koaBody(), async ctx => {  
    const userId = ctx.request.body.userId; // 接收用户ID  
    const newSex = ctx.request.body.newSex; 
  
    // 更新数据库中的用户名
    const updated = await lrx.UPDATESEX(userId, newSex);  
    // console.log(updated.success);
    console.log(newSex)
    if (updated.success) {  
        ctx.body = { success: true, msg: '性别更新成功！' };  
    } else {  
        ctx.body = { success: false, msg: '性别更新失败！' };  
    }  
}); 

router.post('/updatesign', koaBody(), async ctx => {  
    const userId = ctx.request.body.userId; // 接收用户ID  
    const newSign = ctx.request.body.newSign; 
  
    // 更新数据库中的用户名
    const updated = await lrx.UPDATESIGN(userId, newSign);  
    // console.log(updated.success);
    console.log(newSign)
    if (updated.success) {  
        ctx.body = { success: true, msg: '签名更新成功！' };  
    } else {  

        ctx.body = { success: false, msg: '签名更新失败！' };  
    }  
}); 



router.post('/addlevel',koaBody(),async ctx=>{
    const username = ctx.request.body.username;
    const adding = ctx.request.body.add;
    const rows = await lrx.ADDLEVEL(username,adding);
    ctx.body = rows;
    
})

router.post('/rename',koaBody(),async ctx=>{
    const username = ctx.request.body.username;
    const rename = ctx.request.body.aim;
    const rows = await lrx.RENAME(username,rename);
    ctx.body = rows;
    
})

router.post('/repassword', koaBody(), async ctx => {      
    const userId = ctx.request.body.user_id; // 接收用户ID  
    const password = ctx.request.body.password; // 接收原始密码 
    const rows = await lrx.GETUSERBYID(userId,password);
    ctx.body = rows; 
   
});


router.post('/searchuserinfo', koaBody(), async ctx => {
    const userId = ctx.request.body.userId;
    const rows = await lrx.GETUSERBYIDWITHFULLINFO(userId);
    ctx.body = rows;
    
})


router.post('/searchusercollect', koaBody(), async ctx => {
    const username = ctx.request.body.username;
    const rows = await lrx.SEARCHUSERCOLLECT(username);
    ctx.body = rows;
    
})

router.post('/deleteusercollect', koaBody(), async ctx => {
    const collectId = ctx.request.body.collectId;
    const rows = await lrx.DELETEUSERCOLLECT(collectId);
    ctx.body = rows;
    
})


router.post('/deleteusercollect', koaBody(), async ctx => {
    const collectId = ctx.request.body.collectId;
    const rows = await lrx.DELETEUSERCOLLECT(collectId);
    ctx.body = rows;
    
})

router.post('/searchdishs', koaBody(), async ctx => {
    const keyword = ctx.request.body.keyword;
    const rows = await lrx.SEARCHDISHS(keyword);
    ctx.body = rows;
    
})


router.post('/searchdishsdetail', koaBody(), async ctx => {
    const foodname = ctx.request.body.foodname;
    const rows = await lrx.SEARCHDISHSDETAIL(foodname);
    ctx.body = rows;
})

router.post('/searchdishsstep', koaBody(), async ctx => {
    const foodname = ctx.request.body.foodname;
    const rows = await lrx.SEARCHDISHSSTEP(foodname);
    ctx.body = rows;
})



    


module.exports = router